<?php

namespace app\admin\controller;

use app\admin\model\PohtoListImage as PohtoListImageModel;
use app\admin\model\PohtoList as PohtoListModel;
use app\admin\validate\CommonnValidate;
use app\admin\validate\PhotoValidate;
use app\BaseController;

class PhotoImage extends BaseController
{
    protected $middleware = ['Auth'];

    public function list() {
        $params = $this->request->post(['current', 'size', 'id', 'name']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoListImageModel();
        $whereArr = [];
        if(isset($params['id']) && $params['id']) {
            $whereArr[] = ["photo_id", "=", $params['id']];
        }
        $list = $sql
            ->where($whereArr)
            ->order('sort desc')
            ->order('create_at asc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ]);
        if(config('app.imgtype') == 'local') {
            foreach ($list as $k => $item) {
                    $item['image_show'] = $item['image_path'];
            }
        } else {
            foreach ($list as $k => $item) {
                $item['image_show'] = $item['image_url'];
            }
        }
        return result()->success($list);
    }

    public function addEdit() {
        $params = $this->request->post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->edit();
        }
        return $this->add();
    }

    public function add() {
        $params = $this->request->post(['photo_id', 'menu_id', 'img_height', 'img_width', 'image_url', 'sort']);
        $validate = new PhotoValidate();
        if (!$validate->scene('addimage')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sqlList = new PohtoListModel();
        $listInfo = $sqlList->find($params['photo_id']);
        if($listInfo->isEmpty()) {
            return result()->fail("图集不存在", 500);
        }
        $sql = new PohtoListImageModel();
        $info = $sql->where('image_url', $params['image_url'])->findOrEmpty();
        if(!$info->isEmpty()) {
            return result()->fail("图片已存在", 500);
        }
        if(isset($params['img_height']) && $params['img_height']) {
            $status = isset($params['img_width']) && $params['img_width'];
           if(!$status) {
                return result()->fail("图片宽度不能为空", 500);
           }
            $newHeight = $params['img_height'] * 750 / $params['img_width'];
            $newHeight = sprintf("%.2f", $newHeight);
            $params['height'] = $newHeight;
        } else {
            $params['height'] = getImageHeight($params['image_url']);
        }
        if(!$listInfo['cover']) {
            $listInfo->cover = $params['image_url'];
            $listInfo->save();
        }
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function setCover()
    {
        $params = $this->request->post(['photo_id', 'image_url']);
        $validate = new PhotoValidate();
        if (!$validate->scene('addimage')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sqlList = new PohtoListModel();
        $listInfo = $sqlList->find($params['photo_id']);
        if($listInfo->isEmpty()) {
            return result()->fail("图集不存在", 500);
        }
        $listInfo->cover = $params['image_url'];
        $listInfo->save();
        return result()->success("设置成功");
    }

    public function edit() {
        $params = $this->request->post(['id', 'photo_id', 'menu_id', 'img_height', 'img_width', 'image_url', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoListImageModel();
        $info = $sql->find($params['id']);
        if(isset($params['image_url']) && $params['image_url'] && $info['image_url'] != $params['image_url']) {
            if(isset($params['img_height']) && $params['img_height']) {
                $status = isset($params['img_width']) && $params['img_width'];
                if(!$status) {
                    return result()->fail("图片宽度不能为空", 500);
                }
                $newHeight = $params['img_height'] * 750 / $params['img_width'];
                $newHeight = sprintf("%.2f", $newHeight);
                $params['height'] = $newHeight;
            } else {
                $params['height'] = getImageHeight($params['image_url']);
            }
        }
        $sql->update($params, ['id' => $params['id']]);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = $this->request->post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoListImageModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}